﻿//---------------------------------------------------------------
// Copyright (C) 2007 Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------
var map = null;
var coord = null;

function pageLoad() 
{
 if( map != null ) return;
    map = new VEMap("roadtripmap");
    map.LoadMap();
    map.ShowDisambiguationDialog(false);
    map.SetMapStyle(VEMapStyle.Road);
    map.HideDashboard();
    map.SetCenter(new VELatLong(47.22, -122.44));
    RoadTripEx.RouteService.set_defaultUserContext("Default context");
    RoadTripEx.RouteService.set_defaultSucceededCallback(SucceededCallback);
    RoadTripEx.RouteService.set_defaultFailedCallback(FailedCallback);
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    if( parms.length < 1 ) return;
    var pos = parms[0].indexOf('IID=');
    if (pos > -1) 
    {
        RoadTripEx.RouteService.GetRouteList('', parms[0].substring(pos+4));
    }
}

function GetRouteListCallback(result)
{
    PopulateMap(result);
}

function PopulateMap(result)
{
    if( result == null ) return;
    if( result.Start.length >  1 && result.End.length > 1)
    {
        try
        {
             map.GetRoute(result.Start, result.End, [VEDistanceUnit.Miles], [VERouteType.Shortest]);
        }
        catch(e)
        {
            alert(e.description);
           
        }  
        document.getElementById('myTitle').innerHTML = result.Routeidentifier.Name;
    }
    for(var item in result.PushPinList)
    {  
        var pin = new VEShape(VEShapeType.Pushpin,new VELatLong(+result.PushPinList[item].Latitude, +result.PushPinList[item].Longitude));
        pin.SetCustomIcon("<span style='font-family:Arial; font-size:x-small; color:Black; background-color:Silver'><img src='images/iconphoto.png' style='width:20px; height:20px;' /></span>"); 
        pin.SetTitle('<H2 style = position:absolute;top:0px;height:50px;width:250px;left:0px;text-align:center;>' + result.PushPinList[item].Title + '</H2>');
        pin.SetMoreInfoURL(result.PushPinList[item].Moreinfourl);
        pin.SetPhotoURL(result.PushPinList[item].Photourl);
        pin.SetFillColor(new VEColor(0,100,150,0.5));
        map.AddShape(pin);
        description = '<h6 style=font-family:Arial; font-size:x-small;background-image:images/iconphoto.png; color:Black; background-color:Silver> ' + result.PushPinList[item].Description + '</h6>';
        pin.SetDescription(description);
    }
  
}


function SucceededCallback(result, userContext, methodName)
{ 
   var message;
    switch(methodName)
    {
         case ("GetRouteList"):
        {
            GetRouteListCallback(result);
            break;
        }
        
        default:
        {
        }
    }       

}

// Callback function invoked when the call to 
// the Web service methods fails.
function FailedCallback(error, userContext, methodName) 
{
    if(error !== null) alert('An error occurred: ' + error.get_message());
}

